#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"

//********************************************************************************************
void RM692C4_Inter_Demura_Ram(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];	
	u8 RM692C4_Inter_Demura_Ram_Cmd_Table[28]={0xfe,0xA0,0x0B,0x11,0xfe,0xd0,0x58,0x53,0x07,0x8D,0x42,0x81,0x55,0x58,0xFE,0xD2,0x6E,0x20,0xFE,0x40,0x5B,0x01,0x5E,0x40,0x0E,0x40,0xfe,0x00};			
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);		
	for(i=0;i<sizeof(RM692C4_Inter_Demura_Ram_Cmd_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM692C4_Inter_Demura_Ram_Cmd_Table[i];
		buffer1[2] = RM692C4_Inter_Demura_Ram_Cmd_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(15);
		i+=1;
	}					
  buffer[0] = 0x2F;
	buffer[1] = 0x01;		

	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);        																					
}
                                                                   
void RM692C4_Flash_Write_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
//		u32 error_times=0;
		u8 buffer1[3];		
		u8 RM692C4_Flash_Write_Demura_data_Table[14]={0xfe,0x26,0x1B,0x00,0xF2,0x04,0x15,0x00,0x16,0x00,0x17,0x00,0xF2,0x10};		
		delay_ms(2500);//???????1.5s
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(100);	
		for(i=0;i<sizeof(RM692C4_Flash_Write_Demura_data_Table);i++)
		{
				buffer1[0] = 0x02;
				buffer1[1] = RM692C4_Flash_Write_Demura_data_Table[i];
				buffer1[2] = RM692C4_Flash_Write_Demura_data_Table[i+1];
				SSD2828_W_Array(SigMode,channel,buffer1,0);
				delay_ms(5);
				i+=1;
		}	
		delay_ms(2000);
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
		SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); //return package  
		do
		{                        
					buffer1[0] = 0x01;
					buffer1[1] = 0xDB;
					SSD2828_W_Array(SigMode,channel,buffer1,0); 
					delay_ms(6);
					buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);   
					delay_ms(100);
		}while((buffer[4]&0x80)!=0x80);
		delay_ms(5);
		buffer[0] = 0x2F;
		buffer[1] = 0x03;		
		buffer[4] = 0x80;				
	error_tips0:
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);		
}                       
void RM692C4_Flash_Check_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
//	u16 tmp=0;
	u8 buffer1[3];	
	u8 RM692C4_Flash_Check_Read_REG[2]={0xE6,0xE7};
	u8 RM692C4_Flash_Check_Demura_data_Table[10]={0xfe,0x26,0x18,0x1C,0x19,0xF9,0x1A,0x9A,0xF2,0x01};
		
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	for(i=0;i<sizeof(RM692C4_Flash_Check_Demura_data_Table);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM692C4_Flash_Check_Demura_data_Table[i];
			buffer1[2] = RM692C4_Flash_Check_Demura_data_Table[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=1;
		}	
		delay_ms(500);
	
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
		SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);		
		for( i = 0;i<sizeof(RM692C4_Flash_Check_Read_REG);i++)			 	
		{  	
			SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package                           
			buffer1[0] = 0x01;
			buffer1[1] = RM692C4_Flash_Check_Read_REG[i];
			SSD2828_W_Array(SigMode,channel,buffer1,0); 
					delay_ms(6);
			buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);        
		}	
		delay_ms(5);
		buffer[0] = 0x2F;
		buffer[1] = 0x04;				

		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);			 //??check id ?		
}
                                  
void RM692C4_Flash_Erase_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	u8 RM692C4_Flash_Erase_Demura_data_Table[8]={0xfe,0x26,0x1B,0x00,0xF2,0x04,0xf3,0x01};	
	u8 RM692C4_Read_REG=0xDB;	
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	for(i=0;i<sizeof(RM692C4_Flash_Erase_Demura_data_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM692C4_Flash_Erase_Demura_data_Table[i];
		buffer1[2] = RM692C4_Flash_Erase_Demura_data_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		i+=1;
	}
  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
  SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); //return package  
  do
	{                        
     buffer1[0] = 0x01;
     buffer1[1] = RM692C4_Read_REG;
     SSD2828_W_Array(SigMode,channel,buffer1,0); 
		 delay_ms(6);
     buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);      
  }while((buffer[4]&0x80)!=0x80);
  delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x05;		
	buffer[4] = 0x80;	

  SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
  STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //?? ??? Gamma??  :ok  		
}

void RM692C4_Flash_Demura_OTP(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{

}

void RM692C4_Enable_Flash_Control(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
//	u16 tmp=0;
	u8 buffer1[3];		
	u8 RM692C4_Enable_Flash_Control_647[26] = {0xfe, 0x26, 0xf3, 0x02, 0x18, 0x1C, 0x19, 0xF9, 0x1A, 0x9A};
	u8 RM692C4_Enable_Flash_Read_REG[3] = {0xEA,0xEB,0xEC};	
	
	u8 RM692C4_Enable_Flash_Contril_ReloadFlash[26]={0xfe,0x26,0xf3,0x02,0x18,0x1C,0x19,0xFA,0x1A,0xA0};
	u8 RM692C4_Enable_Flash_Dynamic_Switch[26]={0xfe,0x26,0xf3,0x02,0x18,0x1C,0x19,0xF9,0x1A,0x98};
	
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);
//		for(i=0;i<sizeof(RM692A9_Enable_Flash_Control);i++)
//		{
//			buffer1[0] = 0x02;
//			buffer1[1] = RM692A9_Enable_Flash_Control[i];
//			buffer1[2] = RM692A9_Enable_Flash_Control[i+1];
//			SSD2828_W_Array(SigMode,channel,buffer1,0);
//			delay_ms(5);
//			i+=1;
//		}
		switch (buffer[7])
		{				
				case 2:
				{
					for(i=0;i<sizeof(RM692C4_Enable_Flash_Control_647);i++)
					{
						buffer1[0] = 0x02;
						buffer1[1] = RM692C4_Enable_Flash_Control_647[i];
						buffer1[2] = RM692C4_Enable_Flash_Control_647[i+1];
						SSD2828_W_Array(SigMode,channel,buffer1,0);
						delay_ms(15);
						i+=1;
					}
				}break;
				case 6:
				{
					for(i=0;i<sizeof(RM692C4_Enable_Flash_Contril_ReloadFlash);i++)
					{
						buffer1[0] = 0x02;
						buffer1[1] = RM692C4_Enable_Flash_Contril_ReloadFlash[i];
						buffer1[2] = RM692C4_Enable_Flash_Contril_ReloadFlash[i+1];
						SSD2828_W_Array(SigMode,channel,buffer1,0);
						delay_ms(15);
						i+=1;
					}
				}break;
				case 7:
				{
					for(i=0;i<sizeof(RM692C4_Enable_Flash_Dynamic_Switch);i++)
					{
						buffer1[0] = 0x02;
						buffer1[1] = RM692C4_Enable_Flash_Dynamic_Switch[i];
						buffer1[2] = RM692C4_Enable_Flash_Dynamic_Switch[i+1];
						SSD2828_W_Array(SigMode,channel,buffer1,0);
						delay_ms(15);
						i+=1;
					}
				}break;
			default:
				break;
		}
	    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
	    SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
	    for( i = 0; i< 3;i++)			 	
	    {  	
	        SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package                           
	        buffer1[0] = 0x01;
	        buffer1[1] = RM692C4_Enable_Flash_Read_REG[i];
	        SSD2828_W_Array(SigMode,channel,buffer1,0); 
					delay_ms(6);
	        buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);        
	    }	
			//buffer[2]=5;
		delay_ms(5);
	    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //??		
}

void RM692C4_Demura_Function_ON(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	u8 RM692C4_Demura_Function_ON_Table[4] = {0xfe, 0x22, 0x77, 0x00};
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);	
		for(i=0;i<sizeof(RM692C4_Demura_Function_ON_Table);i++)
		{
			buffer1[0] = 0x02;
			buffer1[1] = RM692C4_Demura_Function_ON_Table[i];
			buffer1[2] = RM692C4_Demura_Function_ON_Table[i+1];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=1;
		}	
	  delay_ms(5);
		buffer[0] = 0x2F;
		buffer[1] = 0x07;		
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //????-crc???
}

void RM692C4_Demura_Function_OFF(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	u8 RM692C4_Demura_Function_OFF_Table[4] = {0xfe, 0x22, 0x77, 0x02};
	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	for(i=0;i<sizeof(RM692C4_Demura_Function_OFF_Table);i++)
	{
		buffer1[0] = 0x02;
		buffer1[1] = RM692C4_Demura_Function_OFF_Table[i];
		buffer1[2] = RM692C4_Demura_Function_OFF_Table[i+1];
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
		i+=1;
	}
	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x08;				
  SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
  STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //?? ??? Gamma??  :ok  	
}
//********************************************************************************************
void ProcessForDmuR0D( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(USB_Rx_Buffer[1])
	{
			case 0x01:                                      // 
					RM692C4_Inter_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			//case 0x02:                                      // 
					//RM692C4_Exit_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			//break;						 
			case 0x03:                                      // 
					RM692C4_Flash_Write_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x04:                                      // 
					RM692C4_Flash_Check_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;												
			case 0x05:                                      // 
					RM692C4_Flash_Erase_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			case 0x06:                                      // 
					RM692C4_Enable_Flash_Control(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x07:                                      // 
					RM692C4_Demura_Function_ON(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x08:                                      // 
					RM692C4_Demura_Function_OFF(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;													
			
			default:
				break;												
	}
}
//********************************************************************************************